package cn.zhqr.core.service;

import cn.zhqr.common.bean.response.ErrorCode;
import cn.zhqr.common.config.RiskConfig;
import cn.zhqr.common.exception.ErrorCodeException;
import cn.zhqr.common.utils.DateUtils;
import cn.zhqr.db.model.WithdrawOrder;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.util.Date;

public class WithdrawRiskService {
    public static final WithdrawRiskService instance = new WithdrawRiskService();
    public static final Logger logger = Logger.getLogger(WithdrawRiskService.class);

    public void checkWithdraw(Long agentId, BigDecimal amount){
        if(amount == null || amount.compareTo(BigDecimal.ZERO) <= 0){
            throw new ErrorCodeException(ErrorCode.INVALID_PARAM.code,"参数不合法");
        }
        if(amount.compareTo(RiskConfig.SINGLE_WITHDRAW_AMOUNT) > 0){
            throw new ErrorCodeException(ErrorCode.WITHDRAW_OVER_AMOUNT.code,"单笔最多提现"+RiskConfig.SINGLE_WITHDRAW_AMOUNT+"元");
        }

        Date startTime = DateUtils.getStartTimeOfDay(new Date());
        Date endTime = DateUtils.getEndTimeOfDay(new Date());

        BigDecimal todayTotalWithdrawAmount = WithdrawOrder.dao.sumAmountOfAgentId(agentId,startTime,endTime);
        if(todayTotalWithdrawAmount.compareTo(RiskConfig.SINGLE_DAY_WITHDRAW_AMOUNT) > 0){
            throw new ErrorCodeException(ErrorCode.WITHDRAW_OVER_AMOUNT.code,"单笔最多提现"+RiskConfig.SINGLE_WITHDRAW_AMOUNT+"元");
        }

    }
}
